Divide and Conquer
Wednesday, May 13, 2015
8:40 AM
<<divide-and-conquer-2.pdf>>
Inserted from: <file://\\psf\Dropbox\Schoolworks\Waterloo\4B\CS431\divide-and-conquer-2.pdf>




|
|
|||
|
|
![]() ![]() |
||
|
|
![]() |
||
|
|
![]()
![]()
![]()
![]()
![]()
So is the guess wrong?
No, but sometimes the math just doesn't work out.
|
|
|||||
|
|
…
|
||||
|
|
|
||||
|
|
|||||
|
|
|
||||
|
|
|||||
|
|
|||
|
|
|
||
|
|
|
||
|
|
|
|
|||
|
|
|
||
|
|
|
||
|
|
Recall Math239 solution of recurrence relations
(Homogeneous linear recurrences):
![]()
|
|
|||
|
|
Master Theorem
|
||
|
|
|
||
|
|




![Machine generated alternative text: Dêwkfreid-Ca.in AlgiwthJ DiJóe-and-Conqucr Destin Stratcgy
The Divide-and-Conquer Design Strategy
divide: Given a problem instance I, construct one or more smaller
problem instances, denoted Ii.... . 4 (these are called subproblems).
Usually, we want the size of these subproblems to be small compared to
the size of I, e.g., half the size.
conquer: For 1 j a. solve instance 4 recursively, obtaining solutions
Si Sa.
combine: Given Si.. .. . Sg, use an appropriate combining function to
find the solution S to the problem instance I, i.e.,
S *— combine(St. .... Sa).
- °“ (C) Wtnti,, 2015 61180
Mer
txampie: Uesign of Mergesort
Here, a problem instance consists of an array .4 of n integers, which we
want to sort in increasing order. The size of the problem instance is n.
divide: Split .4 into two subarrays: .4. consists of the first [] elements
in A and AR consists of the last [9J elements in A.
conquer: Run Mergesort on A1 and AR.
combine: After A1 and A have been sorted, use a function Merge to
merge A1 and A into a single sorted array. Recall that this can be done
in time 9(n) with a single pass through Ar and -4R We simply keep
track of the “current” element of A1 and AR, always copying the smaller
one into the sorted array.
D.R. SUman (SCS) CS 341
Winter, 2015 62 / 80](index_files/image035.png)
![Machine generated alternative text: — Mernsart
M ergesort
Algorithm: Mergesart(A: array: n: integer)
il n = 1
then S — A
nL4— F1
flaC [j
AL e [A[1].. . .A[nL]]
else Aa e [A[nL ± 1]... ,4tT1H
5L - Mergesort(AL,nL)
5a — Mergesort(Aa. na)
S e Merge(SL,nz.SR.nR)
return (S, n)
() Wine,, 2015 63/99
r rw—w Mereesort
Analysis of Mergesort
Let T(n) denote the time to run Mergesort on an array of length n.
divide takes time 9(n)
conquer takes time T(F]) —T([j)
combine takes time 9(n)
Recurrence relation:
r(fl)=1T(F1)_T(Li)±9(n) ifn>1
19(1) ifn=1.
D.R. SUman (SCS)
Wntes, 2015 Ut 99](index_files/image036.png)








![Machine generated alternative text: DMik-atd-Ca.iai Algw*I,sJ CL2scst Pair
Checking the Vertical Strip
Algorithm: checkstrip(R. 6)
t t- size(R)
6’ — ¿
[or j <— 1 to t — 1
[or k t— j—1 to rnin{t.j + 7)
-r t— R[j]r
z’*— R[k].x
do do yt-RjJ.y
y’ t— R[kJy ________________
o’ €— rnin{o’. \/(Y — ± (y’ — y)2}
return (å)
Dk Sd.a. (fl
—‚ Clascat PaW
Llosest Pair: Solution 2
Algorithm: CïosestPair2(L. r)
i[i=r then 6foc
mt— (t+r)/2j
& .— ClosestPair2(L m)
comment: -- - Q] ¡s sorted WRT y-coordinates
6 t- CiosestPair2(m ± 1. r)
else comment: Qm ± 1]. . . . Q[rJ ¡s sorted WRT y-coordinates
6 *— min{ÕL, ÒR}
MergeÇë. m. r)
R t— SeÍectCandidates(t, r, ¿. Q[m]x)
¿ 4— CheckStrip(R,ó)
return (à)
I Wine,, 2015 75/80
Wines, 2015 76 ,,‘ 90
Dt SUman (SCS)](index_files/image045.png)









Created with Microsoft OneNote 2010
One place for all your notes and information